extends Node2D

# 初始化顺逆时针前进方向
var dirction=1
# 初始化xy方向、速度
var dirction_x=1
var dirction_y=0
# 初始化地图大小参数

# 初始化组件参数
@onready var sprite_2d = $Sprite2D
@onready var color_rect = $ColorRect
@onready var camera_2d = $Camera2D
@onready var cash = $CanvasLayer/Cash
@onready var canvas_layer = $CanvasLayer
@onready var buy_land_label = $CanvasLayer/buy_land_label

# 玩家在正方形上的位置
var player_square_position

#节点初始化：1、限权初始化
func _enter_tree() -> void:
	# 设置该节点的多人权限，根据名字id初始化，相当于你对player节点的操作只对与你相关id的player有效
	set_multiplayer_authority(name.to_int())
	pass

# Called when the node enters the scene tree for the first time.
func _ready():
	#出生位置初始化
	position = Vector2i(72,72)
	#地图位置初始化
	player_square_position=0
	# UI初始化
	UI_Init()
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta):
	# 如果不是该节点的控制者，则无法移动，直接终止方法
	# 根据设置的节点的权限id，与本身的唯一id作对比，如果一致，则权限正确
	# get_unique_id获取的id即为本机的id标识，在创建客户端或者服务器的时候生成
	if not is_multiplayer_authority():
		# 远程玩家：隐藏其他玩家UI
		canvas_layer.hide()
		# 远程玩家：隐藏其他玩家位置标记
		color_rect.hide()
		# 远程玩家：关闭相机
		camera_2d.enabled = false
		return
	button_action(delta)
	pass

func button_action(delta) -> void:
	# 检测输入移动步数
	var move_step=0
	if Input.is_action_just_pressed("1"):
		move_step = 1
		cash.text="现金：100"  
	elif Input.is_action_just_pressed("2"):
		move_step = 2
		cash.text="现金：200"
	elif Input.is_action_just_pressed("3"):
		move_step = 3
	elif Input.is_action_just_pressed("4"):
		move_step = 4
	elif Input.is_action_just_pressed("5"):
		move_step = 5
	elif Input.is_action_just_pressed("6"):
		move_step = 6
	else:
		move_step = 0
	if Input.is_action_just_pressed("dirction"):
		dirction*=-1
	if move_step>0:
		# 修改map坐标
		player_square_position=max_square_position(player_square_position,0,52)
		player_move(move_step)
	
	
func player_move(step):
	var position_now
	while(step):
		#顶点处方向选择
		position_now=position
		if dirction==1:
			if (position_now.x>=72 and position_now.x<1112) and position_now.y==72:
				dirction_x=1
				dirction_y=0
			elif position_now.x==1112 and (position_now.y>=72 and position_now.y<1112):
				dirction_x=0
				dirction_y=1
			elif (position_now.x>72 and position_now.x<=1112) and position_now.y==1112:
				dirction_x=-1
				dirction_y=0
			elif position_now.x==72 and (position_now.y>72 and position_now.y<=1112):
				dirction_x=0
				dirction_y=-1
		elif dirction==-1:
			if (position_now.x>72 and position_now.x<=1112) and position_now.y==72:
				dirction_x=-1
				dirction_y=0
			elif position_now.x==1112 and (position_now.y>72 and position_now.y<=1112):
				dirction_x=0
				dirction_y=-1
			elif (position_now.x>=72 and position_now.x<1112) and position_now.y==1112:
				dirction_x=1
				dirction_y=0
			elif position_now.x==72 and (position_now.y>=72 and position_now.y<1112):
				dirction_x=0
				dirction_y=1
		
		position.x+=(80*dirction_x)
		position.y+=(80*dirction_y)
		step-=1
		# 完成移动
		if step==0:
			# 判断地图位置做动作
			if is_land_buyed(player_square_position)==false:
				buy_land_label.show()
			
		

func max_square_position(num,min,max):
	if num>=max:
		num-=max
	elif num<min:
		num+=max
	return num
	
func is_land_buyed(player_position):
	if Global.map_own[player_position]=="none":
		return false
	else:
		return true
		
func UI_Init():
	buy_land_label.hide()



func _on_no_buy_land_button_down():
	
	buy_land_label.hide()
	pass # Replace with function body.


func _on_is_buy_land_button_down():
	Global.map_own[player_square_position]=name
	buy_land_label.hide()
	pass # Replace with function body.


